package tool;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.Charset;

public class Message {

	public static final String connect(String pseudo) {
		return "CONNECT/" + pseudo.replaceAll("/", "") + "/";
	}
	
	public static final String exit(String pseudo) {
		return "EXIT/" + pseudo.replaceAll("/", "") + "/";
	}
	
	public static final String guess(String mot) {
		return "GUESS/" + mot.replaceAll("/", "") + "/";
	}
	
	public static final String set_color(int red, int green, int blue) {
		return "SET_COLOR/" + Integer.toString(red) + "/" + Integer.toString(green) + "/" + Integer.toString(blue) + "/";
	}
	
	public static final String set_size(int size) {
		return "SET_SIZE/" + Integer.toString(size) + "/";
	}
	
	public static final String set_line(int x1, int y1, int x2, int y2) {
		return "SET_LINE/" + Integer.toString(x1) + "/" + Integer.toString(y1) + "/" +
					Integer.toString(x2) + "/" + Integer.toString(y2) + "/";
	}

	public static final String set_courbe(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
		return "COURBE/" + Integer.toString(x1) + "/" + Integer.toString(y1) + "/" + 
					Integer.toString(x2) + "/" + Integer.toString(y2) + "/" +
					Integer.toString(x3) + "/" + Integer.toString(y3) + "/" +
					Integer.toString(x4) + "/" + Integer.toString(y4) + "/";
	}
	
	public static final String talk(String message) {
		return "TALK/" + message.replaceAll("/", "") + "/";
	}
	
	public static final String register(String name, String password) {
		return "REGISTER/" + name.replaceAll("/", "") + "/" + password.replaceAll("/", "") + "/";
	}
	
	public static final String login(String name, String password) {
		return "LOGIN/" + name.replaceAll("/", "") + "/" + password.replaceAll("/", "") + "/";
	}
	
	public static final String cheat(String user) {
		return "CHEAT/" + user.replaceAll("/", "") + "/";
	}
	
	public static final String pass() {
		return "PASS/";
	}
	
	public static final String spectateur() {
		return "SPECTATOR/";
	}
	
	
	
	
	/**************** READ/WRITE SOCKET ****************/
	
	
	/**
	 * Methode bloquante, lit le message recu sur la socket
	 * @param socket Socket a ecouter
	 * @return message Message lu sur la socket
	 */	
	public static String readMessage(BufferedReader breader) throws IOException {
		
		String message_recu = "";
		
		try {
			message_recu = breader.readLine();
		} catch (IOException e) {
			throw e;
		}
		
		return message_recu;
	}
	
	
	/**
	 * Ecrit un message dans la socket
	 * @param socket Socket dans laquelle le message doit etre ecrit
	 * @param message Message a envoyer dans la socket
	 */
	public static void writeMessage(Socket socket, String message) {
		
		if (message != null && !"".equals(message.trim())) {
			
			try {
				message = message.trim() + "\n";
				
				socket.getOutputStream().write(message.getBytes(Charset.forName("UTF-8")));
				System.out.println("Message envoyé au serveur : \n -> " + message.trim() + "\n");
			}
			catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
	}
}
